//
//  View.swift
//  Exchange
//
//  Created by rui on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit


enum PopAnimationType {
    case none
    case fromBottom
    case fromStretchTop
}

@objc protocol PopViewDelegate {
    @objc optional func willDismiss()
}

class PopView: UIView {
    var hubColor: UIColor? {
        didSet {
            self.backgroundView.backgroundColor = hubColor
        }
    }
    
    weak var delegate: PopViewDelegate?
    
    typealias Completion = ()->()
    
    var popType: PopAnimationType = .none
    
    private var contentView: UIView?
    private var backgroundView: UIView = {
        let back = UIView()
        back.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        return back
    }()
    
    private var dismissTapGR: UITapGestureRecognizer?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(backgroundView)
    }
    
    init(contentView: String) {
        super.init(frame: CGRect.zero)
        self.addSubview(backgroundView)
        guard let content = Bundle.main.loadNibNamed(contentView, owner: nil, options: nil)?.last as? UIView else {
            return
        }
        self.contentView = content
    }

    init(contentView: UIView) {
        super.init(frame: CGRect.zero)
        self.addSubview(backgroundView)
        self.contentView = contentView
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.addSubview(backgroundView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.backgroundView.frame = self.bounds
    }
    
    func refresh(contentView: Any) {
        self.contentView?.removeFromSuperview()
        self.contentView = nil
        
        if let content = contentView as? UIView {
            self.contentView = content
            return
        }
        if let content = contentView as? String {
            guard let content = Bundle.main.loadNibNamed(content, owner: nil, options: nil)?.last as? UIView else {
                return
            }
            self.contentView = content
        }
    }
    
    func pop(inRect rect: CGRect) {
        guard let content = self.contentView else {
            return
        }
        self.frame = UIScreen.main.bounds
        UIApplication.shared.keyWindow?.addSubview(self)
        content.removeFromSuperview()
        content.frame = rect
        self.addSubview(content)
        
        if popType == .fromBottom {
            self.contentView?.frame = CGRect(x: (self.contentView?.frame.origin.x)!, y: UIScreen.main.bounds.size.height, width: (self.contentView?.frame.size.width)!, height: (self.contentView?.frame.size.height)!)
            self.backgroundView.alpha = 0.0
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.55, initialSpringVelocity: 1.0 / 0.55, options: .curveEaseInOut, animations: {
                if isiPhoneX() {
                    self.contentView?.frame = CGRect(x: (self.contentView?.frame.origin.x)!, y: rect.origin.y - 34, width: (self.contentView?.frame.size.width)!, height: (self.contentView?.frame.size.height)!)
                }else {
                    self.contentView?.frame = CGRect(x: (self.contentView?.frame.origin.x)!, y: rect.origin.y, width: (self.contentView?.frame.size.width)!, height: (self.contentView?.frame.size.height)!)
                }
                self.backgroundView.alpha = 0.2
            }, completion: nil)
        }else if popType == .fromStretchTop {
            self.contentView?.frame = CGRect(x: (self.contentView?.frame.origin.x)!, y: (self.contentView?.frame.origin.y)!, width: (self.contentView?.frame.size.width)!, height: 0)
            self.backgroundView.alpha = 0.0
            
            UIView.animate(withDuration: 0.3) {
                self.contentView?.frame = rect
                self.backgroundView.alpha = 0.2
            }
        }
    }
    
    func dismiss(completion: Completion?) {
        let removeSelf = {
            completion?()
            self.delegate?.willDismiss?()
            self.removeFromSuperview()
        }
        if popType == .fromBottom {
            UIView.animate(withDuration: 0.25, animations: {
                self.contentView?.frame = CGRect(x: (self.contentView?.frame.origin.x)!, y: UIScreen.main.bounds.size.height, width: (self.contentView?.frame.size.width)!, height: (self.contentView?.frame.size.height)!)
                self.backgroundView.alpha = 0.0
            }) {(finished) in
                removeSelf()
            }
        }else if popType == .fromStretchTop {
            UIView.animate(withDuration: 0.25, animations: {
                self.contentView?.frame = CGRect(x: (self.contentView?.frame.origin.x)!, y: (self.contentView?.frame.origin.y)!, width: (self.contentView?.frame.size.width)!, height: 0)
                self.backgroundView.alpha = 0.0
            }) {(finished) in
                removeSelf()
            }
            
        }else {
            removeSelf()
        }
    }
    
    func dismiss() {
        dismiss(completion: nil)
    }
    ///开启点击消失
    func enableAnyTouchDissmiss(){
        if dismissTapGR == nil {
            dismissTapGR = UITapGestureRecognizer(target: self, action: #selector(tapDismiss))
            self.backgroundView.addGestureRecognizer(dismissTapGR!)
        }
    }
    
    static func popView(subView : UIView)-> PopView? {
        guard let supView = subView.superview else {
            return nil
        }
        if let pop = supView as? PopView  {
            return pop
        }
        return popView(subView:supView)
    }
    
    @objc func tapDismiss() {
        dismiss()
    }
    
    deinit {
        dPrint("popview")
    }
    
}
